package com.monas.client;

import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.part.ViewPart;

import com.monas.client.util.UIHelper;

public abstract class AbstractMonasViewPart extends ViewPart {

	public static final String VERSION = "1.0.0";

	public static final String SAVE_LAST = "com.monas.client.command.SaveDocumentCommand";

	public static final String SAVE_HISTORY = "com.monas.client.command.SaveHistoryCommand";

	public static final String SAVE_CHART = "com.monas.client.command.SaveChartCommand";

	protected static final String LAST_DATA = "Last Data";

	protected static final String PERCENTAGE_CHART = "Percentage Chart";

	protected FormToolkit toolkit = null;

	protected ScrolledForm form;

	protected void createForm(Composite parent, String title) {
		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createScrolledForm(parent);
		toolkit.decorateFormHeading(form.getForm());
		form.setText(title);
		form.setImage(getViewImage());

		form.getBody().setLayoutData(UIHelper.fillBoth());
		form.getBody().setLayout(new FillLayout());
	}

	protected void createColumn(Table table, String label, int width) {
		UIHelper.createColumn(table, label, width);
	}

	protected Table createTable(Composite parent, String sectionTitle) {
		Section section = createSection(parent, sectionTitle);

		Composite tabelPanel = toolkit.createComposite(section);
		tabelPanel.setLayout(new FillLayout());

		Table table = toolkit.createTable(tabelPanel, SWT.VIRTUAL
				| SWT.FULL_SELECTION | SWT.MULTI | SWT.V_SCROLL);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		section.setClient(tabelPanel);
		return table;
	}

	protected Section createSection(Composite parent, String title) {
		Section section = toolkit.createSection(parent, Section.TITLE_BAR);
		section.setText(title);
		return section;
	}

	protected void createToolbar() {
		ToolBarManager manager = (ToolBarManager) form.getToolBarManager();
		ToolBar toolBar = manager.createControl(form);
		final Cursor handCursor = new Cursor(Display.getCurrent(),
				SWT.CURSOR_HAND);
		toolBar.setCursor(handCursor);
		// Cursor needs to be explicitly disposed
		toolBar.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				if ((handCursor != null) && (handCursor.isDisposed() == false)) {
					handCursor.dispose();
				}
			}
		});

		manager.add(createContribItem(SAVE_LAST, "icons/save.gif",
				"Save Document from Last Data"));
		manager.add(createContribItem(SAVE_HISTORY, "icons/save_history.gif",
				"Save Document from History Data"));
		manager.add(createContribItem(SAVE_CHART, "icons/save_image.gif",
				"Save Pie Chart into Image File"));
		manager.update(true);
	}

	private CommandContributionItem createContribItem(String id,
			String imagePath, String tooltip) {
		CommandContributionItemParameter savePar = new CommandContributionItemParameter(
				getSite(), null, id, CommandContributionItem.STYLE_PUSH);
		savePar.icon = Activator.getImageDescriptor(imagePath);
		savePar.tooltip = tooltip;
		CommandContributionItem saveItem = new CommandContributionItem(savePar);
		return saveItem;
	}

	public abstract Image getViewImage();

	public abstract void refresh();

	@Override
	public void setFocus() {
	}
}
